using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using UWBGL_XNA_Lib13;
using Microsoft.Xna.Framework;

namespace XNA_SceneNode
{
	public partial class PrimitiveControl : UserControl
	{
        UWB_Primitive mPrimitive;
        String mTextureFileName;
        OpenFileDialog mSelectFile;
  
		public PrimitiveControl()
		{
			InitializeComponent();
           mTextureFileName = null;

           sliderColorChannel.ChangeRangeValues(0.0f, 1.0f, 0.0f);
           mSelectFile = new OpenFileDialog();
           mSelectFile.InitialDirectory = ".";
		}

        public void SetPrimitive(UWB_Primitive p)
        {
            mPrimitive = p;
            PrimitiveSetsControl();
        }

        private void PrimitiveSetsControl()
        {
            if (null != mPrimitive)
            {
                checkBoxTexture.Checked = mPrimitive.isTexturingEnabled();
                mTextureFileName = mPrimitive.getTextureFileName();
                checkBoxBlending.Checked = mPrimitive.isBlendingEnabled();
                checkBoxBoundingVol.Checked = mPrimitive.isDrawingBoundingVolume();
                checkBoxWireFrame.Checked = (mPrimitive.getFillMode() == eFillMode.fmWireframe);
                checkBoxShading.Checked = (mPrimitive.getShadeMode() == eShadeMode.smGouraud);
                checkBoxVisibility.Checked = mPrimitive.isVisible();

                radioObjColor.Checked = true;
                setChannelToControl(getColorChannel(getColorFromPrimitive()));
            }
        }

        private void ControlSetsPrimitive(object sender, EventArgs e)
        {
            if (null != mPrimitive)
            {
                mPrimitive.setShadeMode(checkBoxShading.Checked ? eShadeMode.smGouraud : eShadeMode.smFlat);
                mPrimitive.setFillMode(checkBoxWireFrame.Checked ? eFillMode.fmWireframe : eFillMode.fmSolid);
                mPrimitive.setVisible(checkBoxVisibility.Checked);
                mPrimitive.setDrawBoundingVolume(checkBoxBoundingVol.Checked);
                mPrimitive.enableBlending(checkBoxBlending.Checked);
                mPrimitive.enableTexturing(checkBoxTexture.Checked);
            }
        }
        
        private Microsoft.Xna.Framework.Graphics.Color getColorFromPrimitive()
        {
            Microsoft.Xna.Framework.Graphics.Color toChange;
            if (radioObjColor.Checked)
                toChange = mPrimitive.getFlatColor();
            else
                toChange = mPrimitive.getShadingColor();
            return toChange;
        }

        private void setColorToPrimitive(Microsoft.Xna.Framework.Graphics.Color newColor)
        {
            if (radioObjColor.Checked)
                mPrimitive.setFlatColor(newColor);
            else
                mPrimitive.setShadingColor(newColor);
        }

        private float getColorChannel(Microsoft.Xna.Framework.Graphics.Color theColor)
        {
            float channelV = 0.0f;
            if (radioColorR.Checked)
                channelV = (float)theColor.R / 255.0f;
            else if (radioColorG.Checked)
                channelV = (float) theColor.G / 255.0f;
            else if (radioColorB.Checked)
                channelV = (float) theColor.B / 255.0f;
            else channelV = (float) theColor.A / 255.0f;

            return channelV;
        }

        private Microsoft.Xna.Framework.Graphics.Color setChannelToColor(Microsoft.Xna.Framework.Graphics.Color inputColor, byte colorValue)
        {
            Microsoft.Xna.Framework.Graphics.Color returnColor;
            if (radioColorR.Checked)
                returnColor = new Microsoft.Xna.Framework.Graphics.Color(colorValue, inputColor.G, inputColor.B, inputColor.A);
            else if (radioColorG.Checked)
                returnColor = new Microsoft.Xna.Framework.Graphics.Color(inputColor.R, colorValue, inputColor.B, inputColor.A);
            else if (radioColorB.Checked)
                returnColor = new Microsoft.Xna.Framework.Graphics.Color(inputColor.R, inputColor.G, colorValue, inputColor.A);
            else returnColor = new Microsoft.Xna.Framework.Graphics.Color(inputColor.R, inputColor.G, inputColor.B, colorValue);
            return returnColor;
        }

        private byte getChannelFromControl()
        {
            float colorChannel = sliderColorChannel.GetSliderValue();
            byte colorValue = (byte) (colorChannel * 255);
            return colorValue;
        }

        private void setChannelToControl(float useV)
        {
            sliderColorChannel.SetSliderValue(useV);
            sliderColorChannel.Enabled = true;
        }

        private void sliderColorChannel_Scroll(object sender, EventArgs e)
        {
            setColorToPrimitive(
                   setChannelToColor(getColorFromPrimitive(), getChannelFromControl()));
        }

        private void radioColor_CheckedChanged(object sender, EventArgs e)
        {
            setChannelToControl(getColorChannel(getColorFromPrimitive()));
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            mSelectFile.ShowDialog();
            mTextureFileName = mSelectFile.FileName;
            UWBGL_XNA_Lib13.UWB_Utility.echoToStatusArea("File Selected:" + mTextureFileName);
            mTextureFileName = System.IO.Path.GetFileNameWithoutExtension(mTextureFileName);
            mPrimitive.setTextureFileName(mTextureFileName);
        }
 
	}

}
